
import org.authsum.stitches.*
import org.springframework.web.servlet.ModelAndView
import org.authsum.stitches.external.*
import org.authsum.stitches.services.*

class FoldersController {

	def stitchesService
	
	def addFolderToTagForm = {
	}

	
	def createFolder = { 

			
			
			def folderHolder = new FolderHolder()
			

			return new ModelAndView("/folders/folderForm",  [folderHolder:folderHolder])
			
	}
	
	
	//this is a request to edit a folder, get the foder, and go to the edit screen
	def editFolder = {
			
		def folderRequest = new FolderRequest()
		bindData(folderRequest, params)
		def folderResponse =stitchesService.findFolderById(folderRequest)
		def folderHolder =folderResponse.folderHolders.folderHolder.get(0)
		assert folderResponse.responseCode == '200'

			return new ModelAndView("/folders/folderForm",  [folderHolder:folderHolder])
	}
	
	
	def deleteFolder = {
			
			 def folderRequest = new FolderRequest()
		     		bindData(folderRequest, params)
		     		
		     		
			 		    def response =stitchesService.deleteFolder(folderRequest)
			 		    
			 		    assert response.responseCode == '200'
			flash.message = 'folder deleted'
			
			     chain(action:index)
	}
	
	
	def addFolderToTag = {
			
			 def folderRequest = new FolderRequest()
					bindData(folderRequest, params)
			 		    def response =stitchesService.addFolderToTag(folderRequest)
			 		    
			 		    assert response.responseCode == '200'
			flash.message = 'folder added to tag'
			
			     chain(action:index)
	}
	
	
	
	def folderToContentForm = {
	}
	
	
	
	def addFolderToContent = {
			
			 def folderRequest = new FolderRequest()
					bindData(folderRequest, params)
			 		    def response =stitchesService.addFolderToContent(folderRequest)
			 		    
			 		    assert response.responseCode == '200'
			flash.message = 'folder added to content'
			
			     chain(action:index)
	}
	

	
	def addChildFolderForm = {
	}
	
	//populate the child folders for a given folder
	def findChildFolders = {
			
			 def folderRequest = new FolderRequest()
					bindData(folderRequest, params)
			 		    def response =stitchesService.findChildFolders(folderRequest)
				  [ childFolders:response] 	
		     println "there are folders=" + response.folderHolders.folderHolder.size()
		    // redirect(action:index)	
		     chain(action:index,model:[childFolders:response])

	}
	
	def addChildFolder = {
			
			 def folderRequest = new FolderRequest()
		     		bindData(folderRequest, params)
		     		
		     		
			 		    def response =stitchesService.addChildFolder(folderRequest)
			 		    
			 		    assert response.responseCode == '200'
			flash.message = 'child folder created'
			
			//println "redirecting"
			redirect(controller:'folders',action:index)	
	}
	

    def saveFolder = { 
			
			 def folderRequest = new FolderRequest()
			 			 def folderHolder = new FolderHolder()
			 //folderRequest.folderName = params['foldername']
		     
				if (!folderRequest.folderHolders) {
					def folderHolders = new ArrayOfFolderHolder()
					folderRequest.folderHolders = folderHolders
					folderRequest.folderHolders.folderHolder.add(folderHolder)
				}		 
				bindData(folderHolder, params)
				//TEMP, need to set the id manually
				if (params['id']) {
				folderHolder.id = params['id'].toLong()
				}
				
							
				
			 		    def response =stitchesService.saveFolder(folderRequest)
			 		    
			 		    assert response.responseCode == '200'
			flash.message = 'folder created'
			
			//println "redirecting"
			redirect(controller:'folders',action:index)	
  		
  		
  		
  }
    
    
    def index = { 
			
			 def folderRequest = new FolderRequest()
			 
		    def rootFolders =stitchesService.findRootFolders(folderRequest)

		    println "test"
		     
		     println rootFolders.responseCode
		     
			  [ rootFolders:rootFolders] 		
   		
   		
   		
   }
}
